var fs=require('fs');
load('application');

before(loadPost, {only: ['show', 'edit', 'update', 'destroy']});

action('new', function () {
    this.title = 'New post';
    this.post = new Post;
    render();
});

action(function create() {
	console.log('-----------------');
    var tmpFile = req.form.files.file;
	fs.rename(tmpFile.path,'E:\\work\\catnode\\svn\\public\\data\\'+tmpFile.name,function(err){
		console.log(err);
	});
    console.log(req.form.files);
	
	send('aaaa');
    //Post.create(req.body.Post, function (err, post) {
	/**
	Post.create({title:'title',content:'content',comment:[{name:'name',datetime:new Date()}]}, function (err, post) {
        if (err) {
            flash('error', 'Post can not be created');
            render('new', {
                post: post,
                title: 'New post'
            });
        } else {
            flash('info', 'Post created');
            redirect(path_to.posts());
        }
    });
	**/
});

action(function index() {
    this.title = 'Posts index';
    Post.all(function (err, posts) {
        render({
            posts: posts
        });
    });
});

action(function show() {
    this.title = 'Post show';
    render();
});

action(function edit() {
    this.title = 'Post edit';
    render();
});

action(function update() {
    this.post.updateAttributes(body.Post, function (err) {
        if (!err) {
            flash('info', 'Post updated');
            redirect(path_to.post(this.post));
        } else {
            flash('error', 'Post can not be updated');
            this.title = 'Edit post details';
            render('edit');
        }
    }.bind(this));
});

action(function destroy() {
    this.post.destroy(function (error) {
        if (error) {
            flash('error', 'Can not destroy post');
        } else {
            flash('info', 'Post successfully removed');
        }
        send("'" + path_to.posts() + "'");
    });
});

function loadPost() {
    Post.find(params.id, function (err, post) {
        if (err || !post) {
            redirect(path_to.posts());
        } else {
            this.post = post;
			console.log(post.comment.datetime);
			console.log(post.comment.name);
            next();
        }
    }.bind(this));
}

action (function cap(){
	var browser=require('../helpers/browser.js');
	browser.init();
	browser.render();
	send('hello');
});
